package com.atguigu.cloud.resp;

import com.atguigu.cloud.enums.ReturnCodeEnum;
import jakarta.annotation.Nullable;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 统一返回结果
 *
 * @author Kotori rotroutine@163.com
 * @since 2025/2/27
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Data
@Setter(AccessLevel.PRIVATE)
public class ResultData<T> {
    private final long timestamp = System.currentTimeMillis();
    private String code;
    private String message;
    private T data;

    /**
     * 构造函数
     * @param returnCodeEnum 返回码枚举
     * @param data 数据 可以为空
     */
    public ResultData(ReturnCodeEnum returnCodeEnum, @Nullable T data) {
        this.code = returnCodeEnum.getCode();
        this.message = returnCodeEnum.getMessage();
        this.data = data;
    }

    public static <T> ResultData<T> success() {
        return success(null);
    }

    public static <T> ResultData<T> success(@Nullable T data) {
        return new ResultData<>(ReturnCodeEnum.RC200, data);
    }

    public static <T> ResultData<T> fail(ReturnCodeEnum returnCodeEnum) {
        return fail(returnCodeEnum, null);
    }

    public static <T> ResultData<T> fail(ReturnCodeEnum returnCodeEnum, @Nullable T data) {
        return new ResultData<>(returnCodeEnum, data);
    }
}
